package com.hmall.pay.mq;


import com.hmall.common.client.ItemClient;
import com.hmall.common.client.OrderClient;
import com.hmall.common.dto.Order;
import com.hmall.common.dto.OrderDetail;
import com.hmall.pay.constants.MqConstants;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HotelListener {

    @Autowired
    private OrderClient orderClient;
    @Autowired
    private ItemClient itemClient;
    /**
     * 监听订单超时
     *
     * @param id
     */
    @RabbitListener(queues = MqConstants.ORDER_CLOSE_QUEUE)
    public void listenHotelInsertOrUpdate(String id) {
        //订单的状态，1、未付款 2、已付款,未发货
        // 3、已发货,未确认 4、确认收货，交易成功
        // 5、交易取消，订单关闭 6、交易结束，已评价
        Order order = orderClient.queryOrderById(Long.valueOf(id));
        //int status = orderClient.getStatus(Long.valueOf(id));
        if (order.getStatus()==1){
            OrderDetail orderDetail = orderClient.getOrderDetail(Long.valueOf(id));

            //幂等分布式锁未实现
            int i = orderClient.cancelOrder(Long.valueOf(id));
            itemClient.InventoryRollback(String.valueOf(orderDetail.getItemId()),orderDetail.getNum());
            System.out.println("订单"+order.getId()+"超时未付款,现已回滚，恢复库存");
        }else {
            System.out.println("订单"+order.getId()+"已经付款，无需回滚");
            System.out.println("id = " + id);
        }

    }



}